close all
NumOfStocks = 20;
NumOfBlocks = 2;
load data_2_blocks;
%[CovBlocks, Combinations, SampleSizes, Sigma, StockReturns]=GetCovBlocks(0);

% norms = {'fro', 1, 2};
noise = [4 1];
use_cvx = 0;

CovBlocksNoisy = addNoise(noise, CovBlocks);

iter = 1;
w = [];

for i=0:100,
    w(:, i+1) = [i*0.01; 1-i*0.01];
%    for j=0:100-i,
%        w(:,iter) = [i*0.01; j*0.01; 1-i*0.01-j*0.01];
%        iter = iter+1;
%    end
end

for i=1:size(w,2),
    i
    cvx_begin
        cvx_quiet(true); 
        variable Sigma_hat(NumOfStocks, NumOfStocks) symmetric;
    
        %Define objective function
        f = 0;
        s_max = 0;
        for q=1:NumOfBlocks
            %Add the norm of differences between block covariance and the
            %corresponding block in X.
            f = f + w(q,i)*norm(Sigma_hat(Combinations{q}, Combinations{q})-CovBlocksNoisy{q},2);
            
            s_max = max(s_max, max(max(CovBlocksNoisy{q})));
        end
        minimize (f)
    
        subject to
        Sigma_hat == semidefinite(NumOfStocks);
        Sigma_hat(:) <= s_max;
        Sigma_hat(:) >= -s_max;
    cvx_end
    
    Sigmas_hat{i} = Sigma_hat;
    error(i) = norm(Sigma_hat-Sigma, 2);
    fs(i) = f;
end

[error_min, ind] = min(error);
w_min = w(:,ind);
Sigma_hat_min = Sigmas_hat{ind};